package com.wwj.core.log.service.impl;

import com.wwj.core.log.entity.LogGame;
import com.wwj.core.log.entity.mapper.LogGameMapper;
import com.wwj.core.log.service.ILogGameService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;

@Service
public class LogGameServiceImpl implements ILogGameService {

    @Resource
    private LogGameMapper logGameMapper;

    @Override
    public int createNewLog(LogGame logGame) throws Exception {
        logGame.setCreateTime(Calendar.getInstance().getTime());
        return logGameMapper.save(logGame);
    }

    @Override
    public int updateLog(LogGame logGame) throws Exception {
        LogGame persist = logGameMapper.selectByKey(logGame.getKey());
        if(null == persist)
            return 0;
        if(logGame.getCatchResult() != null && !"".equals(logGame.getCatchResult())){
            //修改抓取结果
            //如果数据库中抓取类型为空则视为自动抓取
            if(persist.getCatchType() == null) {
                persist.setCatchType(LogGame.CATCH_TYPE_AUTO);
                persist.setCatchTime(Long.toString(System.currentTimeMillis()));
            }
            persist.setCatchResult(logGame.getCatchResult());
        }else{
            //修改抓取时间
            persist.setCatchTime(logGame.getCatchTime());
            persist.setCatchType(logGame.getCatchType());
        }
        persist.setUpdateTime(Calendar.getInstance().getTime());
        return logGameMapper.update(persist);
    }
}
